<template>
  <div>
    <!-- 顶部面包屑标识与导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增SPU</el-breadcrumb-item>
      <el-breadcrumb-item>第4步：填写商品详情</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <!-- 添加数据的表单 -->
    <el-form :model="ruleForm" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="基本信息">
        <el-descriptions :title="ruleForm.name">
          <el-descriptions-item label="品牌">
            <el-tag size="small">{{ ruleForm.brandName }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="类别" :span="2">{{ ruleForm.categoryName }}</el-descriptions-item>
          <el-descriptions-item label="标题" :span="3">{{ ruleForm.title }}</el-descriptions-item>
          <el-descriptions-item label="简介" :span="3">{{ ruleForm.description }}</el-descriptions-item>
          <el-descriptions-item label="相册" :span="3">{{ ruleForm.albumName }}</el-descriptions-item>
          <el-descriptions-item label="货号">{{ ruleForm.typeNumber }}</el-descriptions-item>
          <el-descriptions-item label="售价">{{ ruleForm.listPrice }}</el-descriptions-item>
          <el-descriptions-item label="计件单位">{{ ruleForm.unit }}</el-descriptions-item>
          <el-descriptions-item label="库存">{{ ruleForm.stock }}</el-descriptions-item>
          <el-descriptions-item label="库存阈警值">{{ ruleForm.stockThreshold }}</el-descriptions-item>
          <el-descriptions-item label="排序序号">{{ ruleForm.sort }}</el-descriptions-item>
          <el-descriptions-item label="关键词">{{ ruleForm.keywords }}</el-descriptions-item>
          <el-descriptions-item label="标签" :span="2">{{ ruleForm.tags }}</el-descriptions-item>
        </el-descriptions>
      </el-form-item>
      <el-form-item label="商品图片">
        <el-image class="picture-item"
                  v-for="picture in pictureList"
                  :key="picture.id"
                  :src="picture.url"
                  fit="cover"></el-image>
      </el-form-item>
      <el-form-item label="商品详情">
        <div id="wangEditor"></div>
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="backToPreStep()">上一步</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 图片列表
      pictureList: [],
      // 表单
      ruleForm: {
        detail: ''
      },
      // 富文本编辑器
      editor: {}
    };
  },
  methods: {
    // 初始化富文本编辑器
    initWangEditor() {
      this.editor = new this.wangEditor('#wangEditor');
      this.editor.config.zIndex = 1;
      this.editor.create();
    },
    // 返回上一步
    backToPreStep() {
      this.$router.push('add-new-3');
    },
    // 加载本地存储的表单数据
    loadLocalRuleForm() {
      let localRuleFormString = localStorage.getItem('localRuleForm');
      let localRuleForm = JSON.parse(localRuleFormString);

      if (!localRuleForm || !localRuleForm.categoryId) {
        this.$router.push('add-new-1');
        return;
      }

      if (!localRuleForm.brandId) {
        this.$router.push('add-new-2');
        return;
      }

      if (!localRuleForm.albumId) {
        this.$router.push('add-new-3');
        return;
      }

      this.ruleForm = localRuleForm;
      console.log(this.ruleForm);

      this.pictureList = JSON.parse(this.ruleForm.pictures);
    },
    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/spu/add-new';
          console.log('url = ' + url);

          this.ruleForm.detail = this.editor.txt.html();
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);

          let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
          this.axios
              .create({'headers': {'Authorization': loginInfo.token}})
              .post(url, formData).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              let message = '新增SPU成功！';
              this.$alert(message, '提示', {
                confirmButtonText: '确定',
                callback: action => {
                  this.$router.push('/');
                  localStorage.removeItem('localRuleForm');
                }
              });
            } else {
              let message = jsonResult.message;
              this.$alert(message, '警告', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadLocalRuleForm();
    this.initWangEditor();
  }
}
</script>

<style>
.picture-item {
  width: 100px;
  height: 100px;
  margin-right: 16px;
  margin-bottom: 10px;
  border-radius: 3px;
  float: left;
}
</style>